package org.example.impl;

import org.example.DAO.FavoriteDao;
import org.example.entity.Favorite;
import org.example.Util.DataSourceUtil;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class FavoriteDaoImpl implements FavoriteDao {
    private final DataSource dataSource;


    public FavoriteDaoImpl(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public boolean addFavorite(Favorite favorite) throws SQLException {
        String sql = "INSERT INTO favorites (user_id, content_id, content_type, created_at) VALUES (?, ?, ?, NOW())";
        try (Connection conn = dataSource.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            stmt.setInt(1, favorite.getUserId());
            stmt.setInt(2, favorite.getContentId());
            stmt.setString(3, favorite.getContentType());

            int affectedRows = stmt.executeUpdate();
            if (affectedRows > 0) {
                try (ResultSet generatedKeys = stmt.getGeneratedKeys()) {
                    if (generatedKeys.next()) {
                        favorite.setId(generatedKeys.getInt(1));
                    }
                }
                return true;
            }
            return false;
        }
    }

    @Override
    public boolean removeFavorite(int userId, int contentId, String contentType) throws SQLException {
        String sql = "DELETE FROM favorites WHERE user_id = ? AND content_id = ? AND content_type = ?";
        try (Connection conn = dataSource.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, userId);
            stmt.setInt(2, contentId);
            stmt.setString(3, contentType);
            return stmt.executeUpdate() > 0;
        }
    }

    @Override
    public boolean isFavorite(int userId, int contentId, String contentType) throws SQLException {
        String sql = "SELECT COUNT(*) FROM favorites WHERE user_id = ? AND content_id = ? AND content_type = ?";
        try (Connection conn = dataSource.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, userId);
            stmt.setInt(2, contentId);
            stmt.setString(3, contentType);
            try (ResultSet rs = stmt.executeQuery()) {
                return rs.next() && rs.getInt(1) > 0;
            }
        }
    }

    @Override
    public List<Favorite> getUserFavorites(int userId) throws SQLException {
        System.out.println("开始查询用户ID为 " + userId + " 的收藏内容");

        String sql = "SELECT f.id, f.user_id, f.content_id, f.content_type, f.created_at, " +
                "c.title, c.image_url, c.type " +
                "FROM favorites f " +
                "JOIN contents c ON f.content_id = c.id AND f.content_type = c.type " +
                "WHERE f.user_id = ? ORDER BY f.created_at DESC";

        List<Favorite> favorites = new ArrayList<>();
        try (Connection conn = dataSource.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {
            stmt.setInt(1, userId);

            try (ResultSet rs = stmt.executeQuery()) {
                int count = 0;
                while (rs.next()) {
                    count++;
                    Favorite favorite = new Favorite();
                    favorite.setId(rs.getInt("id"));
                    favorite.setUserId(rs.getInt("user_id"));
                    favorite.setContentId(rs.getInt("content_id"));
                    favorite.setContentType(rs.getString("content_type"));
                    favorite.setCreatedAt(rs.getTimestamp("created_at"));
                    favorite.setTitle(rs.getString("title"));
                    favorite.setImageUrl(rs.getString("image_url"));
                    favorites.add(favorite);


                }

            }
        }
        return favorites;
    }
}